const { goodsFormatError } = require("../constants/err.type");

const validator = async (ctx, next) => {
  try {
    // 定义传入参数格式，在传递时自动校验
    ctx.verifyParams({
      goods_name: { type: "string", require: true },
      goods_price: { type: "number", require: true },
    });
  } catch (error) {
    console.error("参数格式错误");
    goodsFormatError.result = error;
    return ctx.app.emit("error", goodsFormatError, ctx);
  }

  await next();
};

module.exports = {
  validator,
};
